#!/usr/bin/env python3
"""
访客数据模型
"""

from datetime import datetime

from sqlalchemy.orm import mapped_column, Mapped
from sqlalchemy.sql.sqltypes import Boolean, DateTime, Integer, String, Text

from app.models import Base


class Visitor(Base):
    """访客信息表"""

    __tablename__ = "visitors"

    # 主键
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)

    # 访客基本信息
    name: Mapped[str] = mapped_column(String(100), nullable=False, comment="访客姓名")
    id_card: Mapped[str] = mapped_column(String(18), unique=True, index=True, comment="身份证号")
    phone: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="手机号码")
    company: Mapped[str | None] = mapped_column(String(200), nullable=True, comment="所属单位")

    # 访问信息
    visit_reason: Mapped[str | None] = mapped_column(String(500), nullable=True, comment="访问事由")
    interviewee: Mapped[str | None] = mapped_column(String(100), nullable=True, comment="被访人")
    interviewee_department: Mapped[str | None] = mapped_column(String(100), nullable=True, comment="被访部门")

    # 时间信息
    planned_visit_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="计划访问时间")
    planned_leave_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="计划离开时间")
    actual_visit_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="实际访问时间")
    actual_leave_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="实际离开时间")

    # 状态信息
    status: Mapped[str] = mapped_column(
        String(20),
        default="pending",
        comment="状态: pending-待审核, approved-已批准, rejected-已拒绝, visiting-访问中, completed-已完成",
    )
    is_special: Mapped[bool] = mapped_column(Boolean, default=False, comment="是否特殊访客")

    # 二维码信息
    qr_code: Mapped[str | None] = mapped_column(String(500), nullable=True, comment="二维码数据")
    qr_code_expire_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="二维码过期时间")

    # 系统信息
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now, comment="创建时间")
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")

    # 备注信息
    remarks: Mapped[str | None] = mapped_column(Text, nullable=True, comment="备注信息")

    def __repr__(self) -> str:
        return f"<Visitor(id={self.id}, name='{self.name}', status='{self.status}')>"
